package com.tlgen.modules.system.service.impl;

import com.tlgen.common.exception.EntityExistException;
import com.tlgen.common.utils.FileUtil;
import com.tlgen.common.utils.PageUtil;
import com.tlgen.common.utils.QueryHelp;
import com.tlgen.common.utils.ValidationUtil;
import com.tlgen.modules.system.domain.NongHe;
import com.tlgen.modules.system.repository.NongHeRepository;
import com.tlgen.modules.system.service.NongHeService;
import com.tlgen.modules.system.service.dto.NongHeDto;
import com.tlgen.modules.system.service.dto.NongHeQueryCriteria;
import com.tlgen.modules.system.service.mapper.NongHeMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Service
@CacheConfig(cacheNames = "nonghe")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class NongHeServiceImpl implements NongHeService {

    private final NongHeRepository nongHeRepository;

    private final NongHeMapper nongHeMapper;

    public NongHeServiceImpl(NongHeRepository nongHeRepository, NongHeMapper nongHeMapper) {
        this.nongHeRepository = nongHeRepository;
        this.nongHeMapper = nongHeMapper;
    }


    /**
     * 不带条件分页查询
     * @param pageable 分页参数
     * @return
     */
    @Override
    @Cacheable
    public Object queryAll(Pageable pageable) {
        return nongHeMapper.toDto(nongHeRepository.findAll(pageable).getContent());
    }


    /**
     * 带条件分页查询
     * @param criteria 条件
     * @param pageable 分页参数
     * @return
     */
    @Override
    @Cacheable
    public Object queryAll(NongHeQueryCriteria criteria, Pageable pageable) {
        Page<NongHe> page = nongHeRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(nongHeMapper::toDto));
    }

    /**
     * 带条件不分页查询
     * @param criteria 条件
     * @return
     */
    @Override
    @Cacheable
    public List<NongHeDto> queryAll(NongHeQueryCriteria criteria) {
        return nongHeMapper.toDto(nongHeRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    /**
     * 新增
     * @param resources /
     * @return
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public NongHeDto create(NongHe resources) {
        return nongHeMapper.toDto(nongHeRepository.save(resources));
    }

    /**
     * 编辑
     * @param resources /
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(NongHe resources) {
        NongHe nongHe = nongHeRepository.findById(resources.getId()).orElseGet(NongHe::new);
        ValidationUtil.isNull(nongHe.getId(),"NongHe","id",resources.getId());

        NongHe nongHe1 = nongHeRepository.findByAgenname(resources.getAgenname());

        if(nongHe1 != null && !nongHe1.getId().equals(nongHe.getId())){
            throw new EntityExistException(NongHe.class,"nonghe",resources.getAgenname());
        }

        nongHe.setAgencode(resources.getAgencode());
        nongHe.setAgenname(resources.getAgenname());
        nongHe.setGrade(resources.getGrade());
        nongHeRepository.save(nongHe);
    }

    /**
     * 根据Id查询
     * @param id /
     * @return
     */
    @Override
    @Cacheable(key = "#p0")
    public NongHeDto findById(long id) {
        NongHe nongHe = nongHeRepository.findById(id).orElseGet(NongHe::new);
        ValidationUtil.isNull(nongHe.getId(),"NongHe","id",id);
        return nongHeMapper.toDto(nongHe);
    }

    /**
     * 删除
     * @param ids /
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void delete(Set<Long> ids) {
        for (Long id : ids) {
            nongHeRepository.deleteById(id);
        }
    }

    /**
     * 导出数据
     * @param response /
     * @throws IOException
     */
    @Override
    public void download(List<NongHeDto> NongHeDtos, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NongHeDto NongHeDto : NongHeDtos) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("经办机构编码", NongHeDto.getAgencode());
            map.put("经办机构名称", NongHeDto.getAgenname());
            map.put("经办机构级别", NongHeDto.getGrade());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

}
